<?php

class Controller_Manage_Category extends  Controller {

	public function action_index() {
		$query = DB::select('id', 'ct_name','ct_desc','create_date')->from('bk_catagories')->order_by('id','desc');
		$result = $query->execute()->as_array();

		$view['data'] = $result;
		return Response::forge(View::forge('manage/category/index',$view), D_HTTP_UPDATE);

	}
	public function action_add() {
		$post = Input::post();
		if ($post) {
			if ($post['category_name'] !== '') {
				$query = DB::insert('bk_catagories');
				$query->columns(array(
						'ct_name',
						'ct_desc'
				));
				$query->values(array(
						$post['category_name'],
						$post['category_desc']
				));
				$query->execute();
				return Response::redirect('manage/category');
			}
			else {
				echo "can nhap ten";
			}
		}
		else {
			return Response::forge(View::forge('manage/category/add'), D_HTTP_UPDATE);
		}
	}

	public function action_edit($id) {
		if ($id) {
			$query = DB::select('id', 'ct_name','ct_desc','create_date')->from('bk_catagories')->where('id',$id);
			$result = $query->execute()->as_array();
			$view['data'] = $result['0'];
			$post = Input::post();
			if ($post) {
				$update_query = DB::update('bk_catagories');
				$update_query->where('id', $id);
				$update_query->set(array(
						'ct_name' => $post['category_name'],
						'ct_desc' => $post['category_desc']
				));
				$update_query->execute();
				return Response::redirect('manage/category');
			}
			return Response::forge(View::forge('manage/category/edit', $view), D_HTTP_UPDATE);

		}
		return Response::redirect('manage/category');
	}

}
